package dao;

import DBU.DBUtil;
import entity.Customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomerDAOImp implements CustomerDAO{
    @Override
    public boolean addCustomer(Customer customer) {
        Connection conn = DBUtil.getConnection();
        String sql = "insert into flower values(?,?,?,?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, customer.getCid());
            ps.setString(2,customer.getName());
            ps.setString(3,customer.getSex());
            ps.setInt(4,customer.getAge());
            ps.setString(5,customer.getPhone());
            ps.setBoolean(6, customer.isVip());
            ps.setString(7, customer.getCuser());
            ps.setString(8, customer.getCpwd());
            ps.executeUpdate();
            return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            DBUtil.close(conn, null, ps, null);
        }
        return false;
    }

    @Override
    public List<Customer> checkCustomer() {
        Connection conn = DBUtil.getConnection();
        String sql = "select * from flower";
        PreparedStatement ps = null;
        try {
            ps  = conn.prepareStatement(sql);
            ps.executeQuery();
            return (List<Customer>) ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<String> buyFlower(int cid) {
        List<String> list = new ArrayList<String>();
        Connection conn = DBUtil.getConnection();
        String sql = "select vip from flower where oid = '"+cid+"'";
        ResultSet rs = null;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                list.add(String.valueOf(rs.getBoolean("vip")));
                list.add(rs.getString("name"));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
